package com.mentaleye.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.mentaleye.bean.StudentsBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.mentaleye.bean.OaClassRelationBean;
import com.mentaleye.dal.dao.OaClassRelationDao;
import com.mentaleye.dal.model.OaClassRelation;
import com.mentaleye.dal.model.OaClassRelationExample;
import com.mentaleye.dal.model.OaClassRelationExample.Criteria;
import com.mentaleye.dal.model.OaClassRelationKey;
import com.mentaleye.service.OaClassRelationService;
import com.mentaleye.util.ObjectUtil;

@Service
public class OaClassRelationServiceImpl implements OaClassRelationService{

	@Autowired private OaClassRelationDao oaClassRelationDao;
	
	@Override
	public boolean addOaClassRelation(OaClassRelationBean oaClassRelationBean) {
		oaClassRelationBean.setCreateTime(new Date());
		OaClassRelation oaClassRelation = (OaClassRelation) ObjectUtil.transfer(oaClassRelationBean, OaClassRelation.class);
		oaClassRelation.setUpdateDesc(oaClassRelationBean.getUpdateDesc());
		return oaClassRelationDao.addOaClassRelation(oaClassRelation);
	}
	
	@Override
	public boolean addOaClassRelationList(List<OaClassRelationBean> list){
		List<OaClassRelation> oaclassRelationlist = new ArrayList<OaClassRelation>();
		for (OaClassRelationBean oaClassRelationBean : list) {
			oaClassRelationBean.setCreateTime(new Date());
			OaClassRelation oaclassRelation = (OaClassRelation) ObjectUtil.transfer(oaClassRelationBean, OaClassRelation.class);
			oaclassRelationlist.add(oaclassRelation);
		}
		return oaClassRelationDao.addOaClassRelationList(oaclassRelationlist);
	}

	@Override
	public boolean updateOaClassRelation(OaClassRelationBean oaClassRelationBean) {
		oaClassRelationBean.setUpdateTime(new Date());
		OaClassRelation oaClassRelation = (OaClassRelation) ObjectUtil.transfer(oaClassRelationBean, OaClassRelation.class);
		return oaClassRelationDao.updateOaClassRelation(oaClassRelation);
	}
	
	@Override
	public boolean updateOaClassRelationList(OaClassRelationBean oaClassRelationBean,List<Integer> personIdList) {
		oaClassRelationBean.setUpdateTime(new Date());
		OaClassRelation oaClassRelation = (OaClassRelation) ObjectUtil.transfer(oaClassRelationBean, OaClassRelation.class);
		Map<String, Object> map = new HashMap<String,Object>();
		map.put("bean", oaClassRelation);
		map.put("personIdList", personIdList);
		return oaClassRelationDao.updateOaClassRelationList(map);
	}
	
	@Override
	public boolean removeOaClassRelation(OaClassRelationKey key){
		return oaClassRelationDao.deleteOaClassRelation(key);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<OaClassRelationBean> queryOaClassRelationById(List<Integer> ids) {
		return  ObjectUtil.transferList(oaClassRelationDao.queryOaClassRelationById(ids), OaClassRelationBean.class);
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<OaClassRelationBean> queryOaClassRelationByPersonId(List<Integer> ids) {
		return (List<OaClassRelationBean>) ObjectUtil.transferList(oaClassRelationDao.queryOaClassRelationByPersonId(ids), OaClassRelationBean.class);
	}
	
	@Override
	public OaClassRelationBean queryOaClassRelationByKey(int oaclassId,int personId){
		OaClassRelationKey key = new OaClassRelationKey();
		key.setOaclassId(oaclassId);
		key.setPersonId(personId);
		return (OaClassRelationBean) ObjectUtil.transfer(oaClassRelationDao.queryOaClassRelationByKey(key), OaClassRelationBean.class);
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<OaClassRelationBean> queryOaClassRelationByIdAndPersonType(OaClassRelationBean oaClassRelationBean) {
		OaClassRelation oaClassRelation = (OaClassRelation) ObjectUtil.transfer(oaClassRelationBean, OaClassRelation.class);
		return ObjectUtil.transferList(oaClassRelationDao.queryOaClassRelationByIdAndPersonType(oaClassRelation), OaClassRelationBean.class);
	}
	
	@Override
	public OaClassRelationBean queryClassIdByPersonId(int personId){
		return (OaClassRelationBean) ObjectUtil.transfer(oaClassRelationDao.selectClassIdByPersonId(personId), OaClassRelationBean.class);
	}

	@Override
	public List<Map<String, Object>> queryClassCount(OaClassRelationBean oaClassRelationBean) {
		OaClassRelation oaClassRelation = (OaClassRelation) ObjectUtil.transfer(oaClassRelationBean, OaClassRelation.class);
		List<Map<String, Object>> list = oaClassRelationDao.queryClassCountByPerson(oaClassRelation);
		return list;
	}
	
	@SuppressWarnings("unchecked")
	public  List<OaClassRelationBean> queryOaclassRelation(OaClassRelationExample example){
		return (List<OaClassRelationBean>) ObjectUtil.transferList(oaClassRelationDao.queryOaclassRelation(example), OaClassRelationBean.class);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<OaClassRelationBean> queryStudentByClassId(Integer oaclassId) {
		return ObjectUtil.transferList(oaClassRelationDao.queryStudentByClassId(oaclassId), OaClassRelationBean.class);
	}

	@SuppressWarnings("unchecked")
	public List<StudentsBean> queryNotClassStudent(){
		return (List<StudentsBean>)ObjectUtil.transferListMap(oaClassRelationDao.selectNotClassStudent(), StudentsBean.class);
	}

	@Override
	public boolean deleteBySystem(Integer classId,List<Integer> personIdList) {
		OaClassRelationExample example=new OaClassRelationExample();
		Criteria criteria = example.createCriteria();
		criteria.andOaclassIdEqualTo(classId);
		criteria.andPersonIdIn(personIdList);
		return this.oaClassRelationDao.deleteByExample(example);
	}

	@Override
	public boolean deleteBySystem(Integer personId, Integer userType) {
		OaClassRelationExample example=new OaClassRelationExample();
		Criteria criteria = example.createCriteria();
		criteria.andPersonIdEqualTo(personId);
		criteria.andPersonTypeEqualTo(userType);
		return this.oaClassRelationDao.deleteByExample(example);
	}
	
	@Override
	public boolean deleteBySystem(List<Integer> personIdList, Integer userType) {
		OaClassRelationExample example=new OaClassRelationExample();
		Criteria criteria = example.createCriteria();
		criteria.andPersonIdIn(personIdList);
		criteria.andPersonTypeEqualTo(userType);
		return this.oaClassRelationDao.deleteByExample(example);
	}
	
	@Override
	public boolean insertByList(List<OaClassRelation> dataList) {
		return this.oaClassRelationDao.addOaClassRelationList(dataList);
	}

	@Override
	public OaClassRelation getByPersonId(Integer personId) {
		OaClassRelation relation=null;
		OaClassRelationExample example=new OaClassRelationExample();
		Criteria criteria = example.createCriteria();
		criteria.andPersonIdEqualTo(personId);
		List<OaClassRelation> relationList = this.oaClassRelationDao.queryOaclassRelation(example);
		if(!relationList.isEmpty()){
			relation=relationList.get(0);
		}
		return relation;
	}
	
	@Override
	public List<OaClassRelation> getListBySystem(Integer classId, List<Integer> personIdList) {
		OaClassRelationExample example=new OaClassRelationExample();
		Criteria criteria = example.createCriteria();
		criteria.andOaclassIdEqualTo(classId);
		criteria.andPersonIdIn(personIdList);
		return this.oaClassRelationDao.queryOaclassRelation(example);
	}

	@Override
	public boolean deleteByPersonIdList(List<Integer> personIdList) {
		OaClassRelationExample example=new OaClassRelationExample();
		Criteria criteria = example.createCriteria();
		criteria.andPersonIdIn(personIdList);
		return this.oaClassRelationDao.deleteByExample(example);
	}

	@Override
	public List<Map<String, Object>> getListByMap(Map<String, Object> map) {
		return this.oaClassRelationDao.getListByMap(map);
	}

	@Override
	public boolean updateOaClassRelationBySelective(OaClassRelation oaClassRelation) {
		OaClassRelationExample example=new OaClassRelationExample();
		Criteria criteria = example.createCriteria();
		criteria.andPersonTypeEqualTo(oaClassRelation.getPersonType());
		criteria.andPersonIdEqualTo(oaClassRelation.getPersonId());
		return this.oaClassRelationDao.updateOaClassRelationByExampleSelective(oaClassRelation, example);
	}
}
